package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/admin/setmeal")
@Slf4j
@RestController
@Api(tags = "套餐管理")
public class SetmealController {

    @Autowired
    SetmealService setmealService;

    /**
     * 新建套餐
     *
     * @return
     */
    @CacheEvict(cacheNames = "setmealCache", key = "#setmealDTO.id")
    @PostMapping
    @ApiOperation("新建套餐")
    public Result save(@RequestBody SetmealDTO setmealDTO) {
        setmealService.save(setmealDTO);

        return Result.success();
    }

    /**
     * 分页查询套餐信息
     *
     * @param setmealPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("分页查询套餐信息")
    public Result<PageResult> page(SetmealPageQueryDTO setmealPageQueryDTO) {
        log.info("套餐，分页查询参数：{}", setmealPageQueryDTO);
        PageResult pageResult = setmealService.page(setmealPageQueryDTO);

        return Result.success(pageResult);
    }

    /**
     * 更改套餐的售卖状态
     * @param status
     * @param id
     * @return
     */
    @CacheEvict(cacheNames = "setmealCache", allEntries = true)
    @ApiOperation("更改套餐的售卖状态")
    @PostMapping("/status/{status}")
    public Result updateStatus(@PathVariable Integer status,Long id) {
        log.info("套餐id：{}，状态：{}",id, status);

        setmealService.updateStatus(id, status);

        return Result.success();
    }

    /**
     * 根据id查询套餐
     * @param id
     * @return
     */
    @ApiOperation("根据id查询套餐")
    @GetMapping("/{id}")
    public Result<SetmealVO> queryInfoById(@PathVariable Long id) {
        log.info("套餐id：{}", id);
        SetmealVO setmealVO = setmealService.queryInfoById(id);

        return Result.success(setmealVO);
    }


    /**
     * 修改套餐信息
     * @param setmealDTO
     * @return
     */
    @CacheEvict(cacheNames = "setmealCache", allEntries = true)
    @ApiOperation("修改套餐信息")
    @PutMapping
    public Result update(@RequestBody SetmealDTO setmealDTO) {
        log.info("套餐信息：{}", setmealDTO);
        setmealService.update(setmealDTO);

        return Result.success();
    }

    /**
     * 删除套餐
     * @param ids
     * @return
     */
    @CacheEvict(cacheNames = "setmealCache", allEntries = true)
    @ApiOperation("删除套餐")
    @DeleteMapping
    public Result delete(@RequestParam List<Long> ids) {
        log.info("删除套餐的ids：{}", ids);
        setmealService.delete(ids);

        return Result.success();
    }

}
